<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkLibrary
// | github 代码仓库：https://github.com/zoujingli/ThinkLibrary
// +----------------------------------------------------------------------

namespace app\liantang\service;

use think\admin\Service;

/**
 * 积分服务
 * Class AdminService
 * @package think\admin\service
 */
class IntegralService extends Service
{

    // 更新个人
    public function updateUserIntegral($uid){
        $user=$this->app->db->name('digit_social')->find($uid);
        if($user){
            //获取积分上限
            $twon_info = $this->app->db->name('digit_organ')->where(['id'=>1])->find();
            $cusconf=json_decode($twon_info['cusconf'],1);
            $max_integ = $cusconf['max_integ'];
            $year = date("Y",time());
            $integral=$this->app->db->name('digit_integral')->where([['uid','=',$uid],['year','=',$year],['type','<>',99],['type','<>',7]])->sum('integral');
            //兑换商品扣得
            $available_score=$this->app->db->name('digit_integral')->where([['uid','=',$uid],['year','=',$year],['type','=',99]])->sum('integral');
            //逾期扣的积分
            $yuqi_score=$this->app->db->name('digit_integral')->where([['uid','=',$uid],['year','=',$year],['type','=',7]])->sum('integral');
            $_int=($integral+$available_score+$yuqi_score)>=0?($integral+$available_score+$yuqi_score):0;
            //先判断总积分是否达到上限 没有 则更新积分
            if ($integral<=$max_integ)
            {
                $this->app->db->name('digit_social')->where(['id'=>$uid])->update(['score'=>$_int]);
            }
        }
    }


    public function getName($uid){
        return $this->app->db->name('digit_social')->find($uid);
    }
}

